package com.taobao.txc.resourcemanager.a.d;

import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.parser.struct.TxcField;
import com.taobao.txc.parser.struct.TxcLine;
import com.taobao.txc.parser.struct.k;
import com.taobao.txc.resourcemanager.b.d;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.jdbc.core.StatementCreatorUtils;

public class a
{
  private static final LoggerWrap a = LoggerInit.logger;
  private final com.taobao.txc.resourcemanager.b.a.a b;
  
  public a(com.taobao.txc.resourcemanager.b.a.a parama)
  {
    this.b = parama;
  }
  
  private void a(StringBuilder paramStringBuilder)
  {
    if (this.b.d() != d.a) {
      paramStringBuilder.append('`');
    }
  }
  
  public void a(TxcLine paramTxcLine, k paramk)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("UPDATE ");
    localStringBuilder.append(paramk.b());
    localStringBuilder.append(" SET ");
    List localList = paramTxcLine.a();
    int i = 1;
    Object localObject1 = localList.iterator();
    Object localObject2;
    String str;
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (TxcField)((Iterator)localObject1).next();
      if ((this.b.d() != d.f) || (!((TxcField)localObject2).b(paramk.h())))
      {
        str = ((TxcField)localObject2).a();
        if (str != null)
        {
          if (i != 0) {
            i = 0;
          } else {
            localStringBuilder.append(", ");
          }
          if ((str.charAt(0) == '`') || (str.charAt(0) == '"'))
          {
            localStringBuilder.append(str);
          }
          else if (this.b.d() == d.c)
          {
            localStringBuilder.append(str.toLowerCase());
          }
          else
          {
            a(localStringBuilder);
            localStringBuilder.append(str);
            a(localStringBuilder);
          }
          localStringBuilder.append("=");
          localStringBuilder.append('?');
        }
      }
    }
    localStringBuilder.append(" WHERE ");
    i = 1;
    localObject1 = localList.iterator();
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (TxcField)((Iterator)localObject1).next();
      if (((TxcField)localObject2).b(paramk.h()))
      {
        str = ((TxcField)localObject2).a();
        if (str != null)
        {
          if (i != 0) {
            i = 0;
          } else {
            localStringBuilder.append(" AND ");
          }
          if ((str.charAt(0) == '`') || (str.charAt(0) == '"'))
          {
            localStringBuilder.append(str);
          }
          else if (this.b.d() == d.c)
          {
            localStringBuilder.append(str.toLowerCase());
          }
          else
          {
            a(localStringBuilder);
            localStringBuilder.append(str);
            a(localStringBuilder);
          }
          localStringBuilder.append(" = ");
          localStringBuilder.append('?');
        }
      }
    }
    PreparedStatement localPreparedStatement = null;
    try
    {
      a.info("sql:" + localStringBuilder.toString());
      localPreparedStatement = this.b.b().prepareStatement(localStringBuilder.toString());
      localObject1 = new ArrayList();
      localObject2 = new ArrayList();
      if (this.b.d() == d.f) {
        b(localList, (List)localObject1, (List)localObject2, paramk);
      } else {
        a(localList, (List)localObject1, (List)localObject2);
      }
      a(localList, (List)localObject1, (List)localObject2, paramk);
      a(localPreparedStatement, (List)localObject1, (List)localObject2);
      localPreparedStatement.execute();
    }
    finally
    {
      if (localPreparedStatement != null) {
        localPreparedStatement.close();
      }
    }
  }
  
  public void a(TxcLine paramTxcLine, k paramk, String paramString)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("UPDATE ");
    localStringBuilder.append(paramk.b());
    localStringBuilder.append(" SET ");
    localStringBuilder.append(paramString);
    localStringBuilder.append(" WHERE ");
    int i = 1;
    List localList = paramTxcLine.a();
    Object localObject1 = localList.iterator();
    Object localObject2;
    Object localObject3;
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (TxcField)((Iterator)localObject1).next();
      if (((TxcField)localObject2).b(paramk.h()))
      {
        localObject3 = ((TxcField)localObject2).a();
        if (localObject3 != null)
        {
          if (i != 0) {
            i = 0;
          } else {
            localStringBuilder.append(" AND ");
          }
          if ((((String)localObject3).charAt(0) == '`') || (((String)localObject3).charAt(0) == '"'))
          {
            localStringBuilder.append((String)localObject3);
          }
          else if (this.b.d() == d.c)
          {
            localStringBuilder.append(((String)localObject3).toLowerCase());
          }
          else
          {
            a(localStringBuilder);
            localStringBuilder.append((String)localObject3);
            a(localStringBuilder);
          }
          localStringBuilder.append(" = ");
          localStringBuilder.append('?');
        }
      }
    }
    localObject1 = null;
    try
    {
      localObject1 = this.b.b().prepareStatement(localStringBuilder.toString());
      localObject2 = new ArrayList();
      localObject3 = new ArrayList();
      a(localList, (List)localObject2, (List)localObject3, paramk);
      a((PreparedStatement)localObject1, (List)localObject2, (List)localObject3);
      ((PreparedStatement)localObject1).execute();
    }
    finally
    {
      if (localObject1 != null) {
        ((PreparedStatement)localObject1).close();
      }
    }
  }
  
  public void b(TxcLine paramTxcLine, k paramk)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    List localList = paramTxcLine.a();
    localStringBuilder.append("INSERT INTO ");
    localStringBuilder.append(paramk.b());
    localStringBuilder.append("(");
    int i = 1;
    Object localObject1 = localList.iterator();
    Object localObject2;
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (TxcField)((Iterator)localObject1).next();
      String str = ((TxcField)localObject2).a();
      if (str != null)
      {
        if (i != 0) {
          i = 0;
        } else {
          localStringBuilder.append(", ");
        }
        if ((str.charAt(0) == '`') || (str.charAt(0) == '"'))
        {
          localStringBuilder.append(str);
        }
        else if (this.b.d() == d.c)
        {
          localStringBuilder.append(str.toLowerCase());
        }
        else
        {
          a(localStringBuilder);
          localStringBuilder.append(str);
          a(localStringBuilder);
        }
      }
    }
    localStringBuilder.append(")");
    localStringBuilder.append(" VALUES ");
    localStringBuilder.append("(");
    i = 1;
    localObject1 = localList.iterator();
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (TxcField)((Iterator)localObject1).next();
      if (i != 0) {
        i = 0;
      } else {
        localStringBuilder.append(", ");
      }
      localStringBuilder.append('?');
    }
    localStringBuilder.append(")");
    PreparedStatement localPreparedStatement = null;
    try
    {
      localPreparedStatement = this.b.b().prepareStatement(localStringBuilder.toString());
      localObject1 = new ArrayList();
      localObject2 = new ArrayList();
      a(localList, (List)localObject1, (List)localObject2);
      a(localPreparedStatement, (List)localObject1, (List)localObject2);
      localPreparedStatement.execute();
    }
    finally
    {
      if (localPreparedStatement != null) {
        localPreparedStatement.close();
      }
    }
  }
  
  public void a(List<TxcLine> paramList, k paramk)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("DELETE FROM ");
    localStringBuilder.append(paramk.b());
    localStringBuilder.append(" WHERE ");
    List localList = paramk.g();
    int i = 1;
    Object localObject1 = localList.iterator();
    Object localObject2;
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (String)((Iterator)localObject1).next();
      if (i != 0) {
        i = 0;
      } else {
        localStringBuilder.append(" AND ");
      }
      localStringBuilder.append((String)localObject2);
      localStringBuilder.append(" = ");
      localStringBuilder.append("?");
    }
    localObject1 = null;
    try
    {
      localObject1 = this.b.b().prepareStatement(localStringBuilder.toString());
      localObject2 = paramList.iterator();
      while (((Iterator)localObject2).hasNext())
      {
        TxcLine localTxcLine = (TxcLine)((Iterator)localObject2).next();
        ArrayList localArrayList1 = new ArrayList();
        ArrayList localArrayList2 = new ArrayList();
        Iterator localIterator = localTxcLine.a().iterator();
        while (localIterator.hasNext())
        {
          TxcField localTxcField = (TxcField)localIterator.next();
          if (localTxcField.b(paramk.h()))
          {
            localArrayList1.add(localTxcField.c());
            localArrayList2.add(Integer.valueOf(localTxcField.b()));
            a((PreparedStatement)localObject1, localArrayList1, localArrayList2);
          }
        }
        ((PreparedStatement)localObject1).addBatch();
      }
      ((PreparedStatement)localObject1).executeBatch();
    }
    finally
    {
      if (localObject1 != null) {
        ((PreparedStatement)localObject1).close();
      }
    }
  }
  
  private void a(List<TxcField> paramList, List<Object> paramList1, List<Integer> paramList2)
  {
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
    {
      TxcField localTxcField = (TxcField)localIterator.next();
      paramList1.add(localTxcField.c());
      paramList2.add(Integer.valueOf(localTxcField.b()));
    }
  }
  
  private void a(List<TxcField> paramList, List<Object> paramList1, List<Integer> paramList2, k paramk)
  {
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
    {
      TxcField localTxcField = (TxcField)localIterator.next();
      if (localTxcField.b(paramk.h()))
      {
        paramList1.add(localTxcField.c());
        paramList2.add(Integer.valueOf(localTxcField.b()));
      }
    }
  }
  
  private void b(List<TxcField> paramList, List<Object> paramList1, List<Integer> paramList2, k paramk)
  {
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
    {
      TxcField localTxcField = (TxcField)localIterator.next();
      if (!localTxcField.b(paramk.h()))
      {
        paramList1.add(localTxcField.c());
        paramList2.add(Integer.valueOf(localTxcField.b()));
      }
    }
  }
  
  private void a(PreparedStatement paramPreparedStatement, List<Object> paramList, List<Integer> paramList1)
  {
    for (int i = 0; i < paramList.size(); i++) {
      a(paramPreparedStatement, i + 1, ((Integer)paramList1.get(i)).intValue(), paramList.get(i));
    }
  }
  
  public static void a(PreparedStatement paramPreparedStatement, int paramInt1, int paramInt2, Object paramObject)
  {
    if (paramObject == null)
    {
      paramPreparedStatement.setNull(paramInt1, paramInt2);
    }
    else
    {
      SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      if ((paramInt2 == 91) || (paramInt2 == 92) || (paramInt2 == 93)) {
        try
        {
          if (paramInt2 == 93) {
            paramPreparedStatement.setTimestamp(paramInt1, paramObject == null ? null : new Timestamp(Long.parseLong(paramObject.toString())));
          } else if (paramInt2 == 92) {
            paramPreparedStatement.setTime(paramInt1, paramObject == null ? null : new Time(Long.parseLong(paramObject.toString())));
          } else {
            paramPreparedStatement.setDate(paramInt1, paramObject == null ? null : new java.sql.Date(Long.parseLong(paramObject.toString())));
          }
        }
        catch (NumberFormatException localNumberFormatException)
        {
          try
          {
            java.util.Date localDate = localSimpleDateFormat.parse(paramObject.toString());
            paramPreparedStatement.setDate(paramInt1, new java.sql.Date(localDate.getTime()));
          }
          catch (ParseException localParseException)
          {
            throw new RuntimeException(localParseException);
          }
        }
      } else {
        StatementCreatorUtils.setParameterValue(paramPreparedStatement, paramInt1, paramInt2, paramObject);
      }
    }
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.resourcemanager.a.d.a
 * JD-Core Version:    0.7.0.1
 */